{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #String to URL LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: replaceSpaces"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #URL化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>URL化。编写一种方法，将字符串中的空格全部替换为<code>%20</code>。假定该字符串尾部有足够的空间存放新增字符，并且知道字符串的“真实”长度。（注：用<code>Java</code>实现的话，请使用字符数组实现，以便直接在数组上操作。）</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入</strong>：\"Mr John Smith    \", 13\n",
    "<strong>输出</strong>：\"Mr%20John%20Smith\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入</strong>：\"               \", 5\n",
    "<strong>输出</strong>：\"%20%20%20%20%20\"\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>字符串长度在 [0, 500000] 范围内。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [string-to-url-lcci](https://leetcode.cn/problems/string-to-url-lcci/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [string-to-url-lcci](https://leetcode.cn/problems/string-to-url-lcci/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"Mr John Smith    \"\\n13', '\"               \"\\n5']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        ret = ''\n",
    "        i = 0\n",
    "        for s in S:\n",
    "            if i >= length: break\n",
    "            ret += (s if s != ' ' else '%20')\n",
    "            i += 1\n",
    "        return ret\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        res = \"\"\n",
    "        for i in range(length):\n",
    "            res += S[i] if S[i] != ' ' else \"%20\"\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        s = \"\"\n",
    "        cnt = 0\n",
    "        for i in S:\n",
    "            if cnt == length:\n",
    "                break\n",
    "            if i == \" \":\n",
    "                s += \"%20\"\n",
    "            else:\n",
    "                s += i\n",
    "            cnt += 1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        return S[:length].replace(' ' , '%20')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        return S[0:length].replace(' ', '%20')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        return S[0:length].replace(' ', '%20')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        n = len(S)\n",
    "        T = S[:length].replace(' ', '%20')\n",
    "        # T += ' ' * (n - len(T))\n",
    "        return T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        real_s=S[:length]\n",
    "        real_s_list=list(real_s)\n",
    "        for i in range(len(real_s_list)):\n",
    "            if real_s_list[i]==' ':\n",
    "                real_s_list[i]=\"%20\"\n",
    "        # for i in real_s_list:\n",
    "        #     print(i)\n",
    "        # print(''.join(real_s_list))\n",
    "        return ''.join(real_s_list)\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        real_length = length\n",
    "        for idx in range(length):\n",
    "            if S[idx] == ' ':\n",
    "                S += '%20'\n",
    "                real_length += 2\n",
    "            else:\n",
    "                S += S[idx]\n",
    "        return S[-real_length:]\n",
    "        # return \"%20\".join(S[:length].split(\" \"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        real_length = length\n",
    "        for idx in range(length):\n",
    "            if S[idx] == ' ':\n",
    "                S += '%20'\n",
    "                real_length += 2\n",
    "            else:\n",
    "                S += S[idx]\n",
    "        return S[-real_length:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        s = S[:length]\n",
    "        ans = list()\n",
    "        for c in s:\n",
    "            if c == ' ':\n",
    "                ans.append('%20')\n",
    "            else:\n",
    "                ans.append(c)\n",
    "        return \"\".join(ans)\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "2022.12.15 第一次提交，根据示例1，2写的代码，忽略了其他的场景\n",
    "经过自己的测试，完全和自己想的不一样\n",
    "        ans = S.split()\n",
    "        if len(ans) == 0:\n",
    "            return \"%20\" * length\n",
    "        real_length = sum(len(ch) for ch in ans)\n",
    "\n",
    "        if real_length == length:\n",
    "            return \"\".join(ans)\n",
    "        return \"%20\".join(ans)\n",
    "\n",
    "题意中并没有明确表示20%应该优先哪个位置\n",
    "\n",
    "异常示例1. \n",
    "\"Mr John Smith                 \"\n",
    "14\n",
    "\n",
    "\"Mr%20John%20Smith%20\"\n",
    "\n",
    "异常示例2.\n",
    "\"Mr John Smith                 \"\n",
    "14\n",
    "\n",
    "\"Mr%20John%20Smith%20%20\"\n",
    "\n",
    "异常示例3. \n",
    "\"          Mr John Smith                 \"\n",
    "15\n",
    "\"%20%20%20%20%20%20%20%20%20%20Mr%20Jo\"\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        s = S[0:length]\n",
    "        res = []\n",
    "        for c in s:\n",
    "            if c == ' ':\n",
    "                res.append(\"%20\")\n",
    "            else:\n",
    "                res.append(c)\n",
    "        \n",
    "        res_str = ''.join(res)\n",
    "        return res_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        # S_list = S.split()\n",
    "        # n = 2*len(S_list)-1\n",
    "        # i = 1\n",
    "        # while i < n:\n",
    "        #     S_list.insert(i, '%20')\n",
    "        #     i += 2\n",
    "        # print(S_list)\n",
    "        # return ''.join(S_list)\n",
    "\n",
    "\n",
    "        # num = (len(S) - length) / 3\n",
    "        # for i in range(len(S)):\n",
    "        #     print(i, S[i])\n",
    "        #     if S[i] == ' ':\n",
    "        #         S[i] = \n",
    "        # print(S)\n",
    "        print(S)\n",
    "        # end_blank = len(S) - length\n",
    "        S = S[0:length]\n",
    "        print(S)\n",
    "        res = []\n",
    "        for item in S:\n",
    "            if item != ' ':\n",
    "                res.append(item)\n",
    "            else:\n",
    "                res.append('%20')\n",
    "        return ''.join(res)\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        res = []\n",
    "        for c in S[:length]:\n",
    "            if c == \" \":\n",
    "                c = '%20'\n",
    "            res.append(c)\n",
    "        return ''.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        res = []\n",
    "        for c in S[:length]:\n",
    "            if c == ' ':\n",
    "                c = '%20'\n",
    "            res.append(c)\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        result = []\n",
    "        for i in range(length):\n",
    "            if S[i] == ' ':\n",
    "                result.append('%20')\n",
    "            else:\n",
    "                result.append(S[i])\n",
    "        return ''.join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        n = len(S)\n",
    "        for i in range(n):\n",
    "            if S[i] != ' ':\n",
    "                temp =  S[i:i+length+1]\n",
    "        temp = S[:length]\n",
    "        return \"%20\".join(temp.split(' '))\n",
    "      \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        S = S[:length]\n",
    "        string = S.split()\n",
    "        res = '%20'.join(string)\n",
    "        ans = ''\n",
    "        for ele in S:\n",
    "            if ele == \" \":\n",
    "                ans += \"%20\"\n",
    "            else:\n",
    "                ans += ele\n",
    "        #for ele in string:\n",
    "        print(ans)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        space_last = len(S) - length\n",
    "        S_list = [*map(str, S.split())]\n",
    "        result = ''\n",
    "\n",
    "        if len(S_list) == 0:\n",
    "            return '%20'*length\n",
    "        for i in range(length):\n",
    "            if S[i] == ' ':\n",
    "                result += '%20'\n",
    "            else:\n",
    "                result += S[i]\n",
    "        return result\n",
    "\n",
    "        # if S[0] == ' ':\n",
    "        #     for tmp in S:\n",
    "        #         if tmp == ' ':\n",
    "        #             result += '%20'\n",
    "        #         else:\n",
    "        #             break\n",
    "        \n",
    "        # result += '%20'.join(S_list)\n",
    "        # return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        space_last = len(S) - length\n",
    "        S_list = [*map(str, S.split())]\n",
    "        result = ''\n",
    "\n",
    "        if len(S_list) == 0:\n",
    "            return '%20'*length\n",
    "        for i in range(length):\n",
    "            if S[i] == ' ':\n",
    "                result += '%20'\n",
    "            else:\n",
    "                result += S[i]\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "\n",
    "        result = ['%20' if i == ' ' else i for i in S[:length]]\n",
    "        res = ''.join(result)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        result = []\n",
    "        for x in S.rstrip():\n",
    "            if x == \" \":\n",
    "                result.append(\"%20\")\n",
    "            else:\n",
    "                result.append(x)\n",
    "        \n",
    "        if len(result) < length:\n",
    "            temp = [\"%20\"] * (length - len(result))\n",
    "            result += temp\n",
    "        \n",
    "        return \"\".join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        cur = -1\n",
    "        ret = [''] * length\n",
    "        for c in S[length - 1::-1]:\n",
    "            ret[cur] = '%20' if c == ' ' else c\n",
    "            cur -= 1\n",
    "        return ''.join(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        a=[]\n",
    "        for i in range(length):\n",
    "            if S[i]!=' ':\n",
    "                a.append(S[i])\n",
    "            else:\n",
    "                a.append('%20')\n",
    "        return ''.join(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        S = S[0:length]\n",
    "        s_list = S.split(' ')\n",
    "        return ('%20').join(s_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        return \"%20\".join(S[:length].split(\" \"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        # return S[:length].replace(' ','%20')\n",
    "        return '%20'.join(S[:length].split(' '))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, s: str, length: int) -> str:\n",
    "        ans = []\n",
    "        for c in s:\n",
    "            if c == \" \":\n",
    "                if length > 0:\n",
    "                    ans.append(\"%20\")\n",
    "            else:\n",
    "                ans.append(c)\n",
    "            length -= 1\n",
    "            print(c, length)\n",
    "        return \"\".join(ans)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, s1: str, s2: int) -> str:\n",
    "        new_s = s1[:s2]\n",
    "        print(new_s)\n",
    "        s1_list = list(new_s)\n",
    "        for i in range(s2):\n",
    "            if s1_list[i] == \" \":\n",
    "                s1_list[i] = \"%20\"\n",
    "\n",
    "        s = ''.join(s1_list)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        sarr = []\n",
    "        S = S[:length]\n",
    "        for idx, ss in enumerate(S):\n",
    "            if ss == ' ':\n",
    "                sarr.append('%20')\n",
    "            else:\n",
    "                sarr.append(ss)\n",
    "        return ''.join(sarr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        sarr = []\n",
    "        S = S[:length]\n",
    "        for idx, ss in enumerate(S):\n",
    "            if ss == ' ':\n",
    "                sarr.append('%20')\n",
    "            else:\n",
    "                sarr.append(ss)\n",
    "        return ''.join(sarr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        p1=length-1\n",
    "        S=list(S[:length+S[:length].count(' ')*2])\n",
    "        p2=len(S)-1\n",
    "        while p1!=p2:\n",
    "            if S[p1]!=' ':\n",
    "                S[p2]=S[p1]\n",
    "                p2-=1\n",
    "            else:\n",
    "                S[p2-2],S[p2-1],S[p2]='%','2','0'\n",
    "                p2-=3\n",
    "            p1-=1\n",
    "        return ''.join(S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        return \"%20\".join(S[:length].split(\" \")) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        return \"%20\".join(S[:length].split(\" \"))  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        s_list = S[0: length].split(' ')\n",
    "        return '%20'.join(s_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        S = S[0:length]\n",
    "        print(S)\n",
    "        new_list = S.split(' ')\n",
    "\n",
    "        res = ''\n",
    "        if len(new_list) == 0 and length > 0:\n",
    "            res = '%20' * length\n",
    "        if len(new_list) > 0:\n",
    "            res = '%20'.join(new_list)\n",
    "\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "        # # 1. 去除首尾空格\n",
    "        # new_str = S.strip(' ')\n",
    "        # new_list = new_str.split(' ')\n",
    "\n",
    "        # res = ''\n",
    "        # if len(new_list) == 0 and length > 0:\n",
    "        #     res = '%20' * length\n",
    "        # if len(new_list) > 0 and \n",
    "\n",
    "\n",
    "        # res = '%20'.join(new_list)\n",
    "\n",
    "        # return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        s = S[:length]\n",
    "        return '%20'.join(s.split(' '))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        # return S[:length].replace(\" \", \"%20\")\n",
    "        return re.sub(\" \", \"%20\", S[:length])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        s1=list(S)\n",
    "        s2=''\n",
    "        for i in range(0,length):\n",
    "            if s1[i] != ' ':\n",
    "                s2+=s1[i]\n",
    "            else:\n",
    "                s2+='%20'\n",
    "        return s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        a=list(S)\n",
    "        ret=''\n",
    "        for i in range(length):\n",
    "            if a[i] == ' ':\n",
    "                a[i] = '%20'\n",
    "            ret += (str(a[i]))\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, astr: str, length: int) -> str:\n",
    "        if astr == \"\":\n",
    "            print(\"None data\")\n",
    "            return \n",
    "        lst = list(astr)\n",
    "        newstr = \"\"\n",
    "        return_str = \"\"\n",
    "        num = 0\n",
    "    \n",
    "        if len(astr) > length:\n",
    "            newstr = astr[0:length]\n",
    "    \n",
    "        for item in newstr:\n",
    "            if item == \" \":\n",
    "                num += 1\n",
    "            \n",
    "        if num == length:\n",
    "            #print(\"all space process\")\n",
    "            return_str = newstr.replace(\" \", \"%20\")\n",
    "            return return_str  \n",
    "        if len(astr) == length:\n",
    "            return astr\n",
    "\n",
    "        return_str = newstr[:length].replace(\" \", \"%20\")\n",
    "        return return_str\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "            t=list(S)\n",
    "            a=0\n",
    "            for q in range(len(t)):\n",
    "                a=a+1\n",
    "                if t[q]==\" \":\n",
    "                   t[q]=\"%20\"\n",
    "                if a==length:\n",
    "                  break\n",
    "            return ''.join(t).strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        # 双指针？？\n",
    "        # 优化1：计算空格数量，一次挪到位；不对\n",
    "        count = 0\n",
    "        for i in range(length):\n",
    "            if S[i] == ' ':\n",
    "                count += 1\n",
    "        n = length + count * 2\n",
    "        slist = [' '] * n\n",
    "        k = 0\n",
    "        for i in range(length):\n",
    "            if S[i] != ' ':\n",
    "                slist[k] = S[i]\n",
    "                k += 1\n",
    "            else:\n",
    "                slist[k] = '%'\n",
    "                slist[k+1] = '2'\n",
    "                slist[k+2] = '0'\n",
    "                k += 3\n",
    "        new_s = ''.join(slist)\n",
    "        return new_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        S = S[:length].split(' ')\n",
    "        print(S)\n",
    "        # S_keep = ''\n",
    "        # for i in range(length):\n",
    "        #     if S[i] == ' ':\n",
    "        #         S_keep += '%20'\n",
    "        #     else:\n",
    "        #         S_keep += S[i]\n",
    "        S_keep = '%20'.join(S)\n",
    "        return S_keep\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        p = S[:length].split(' ')\n",
    "        print(p)\n",
    "        return '%20'.join(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    import re\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        return re.sub(' ', '%20', S[:length])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        \"\"\"\n",
    "\n",
    "        :param S:\n",
    "        :param length:\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        ret = []\n",
    "        for ch in S[: length]:\n",
    "            if ch != \" \":\n",
    "                ret.append(ch)\n",
    "            else:\n",
    "                ret.append(\"%20\")\n",
    "        print(ret)\n",
    "\n",
    "        return \"\".join(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        s2 = S.replace(\" \", \"\")\n",
    "        k = length - len(s2)\n",
    "        SS = list(S)\n",
    "        for i in range(length):\n",
    "            if SS[i] == \" \":\n",
    "                if k > 0:\n",
    "                   SS[i] = \"%20\"\n",
    "                else:\n",
    "                    break\n",
    "        return \"\".join(SS).replace(\" \", \"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        s = S.rstrip().split(' ')\n",
    "        re = length - sum([len(s[i]) for i in range(len(s))]) - len(s) + 1\n",
    "        #print(length)\n",
    "        #print([len(s[i]) for i in range(len(s))])\n",
    "        #print(re)\n",
    "        if s:\n",
    "            res = s[0]\n",
    "            for i in range(1, len(s)):\n",
    "                res += '%20'\n",
    "                res += s[i]\n",
    "        res += '%20' * re\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        ss=[]\n",
    "        for i in range(length):\n",
    "             ss.append(S[i])\n",
    "        s1=(''.join(ss)).split(' ')\n",
    "        return '%20'.join(s1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        l,r=length-1,len(S)-1\n",
    "        S=list(S)\n",
    "        while l!=r and l>=0 and r>=0:\n",
    "            if S[l]==\" \":\n",
    "                S[r],S[r-1],S[r-2]=\"0\",\"2\",\"%\"\n",
    "                l-=1\n",
    "                r-=3\n",
    "            else:\n",
    "                S[r]=S[l]\n",
    "                l-=1\n",
    "                r-=1\n",
    "        print(l,r)\n",
    "        return \"\".join(S[r+1:]) if l!=r else \"\".join(S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        S_list = list(S[: length])\n",
    "        ans = []\n",
    "        for i in range(len(S_list)):\n",
    "            if S_list[i] == ' ':\n",
    "                ans.append('%20')\n",
    "            else:\n",
    "                ans.append(S_list[i])\n",
    "        return (''.join(ans))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        newlen = length\n",
    "        blank = 0\n",
    "        for c in S[:length]:\n",
    "            if c == ' ':\n",
    "                blank += 1\n",
    "        newlen += blank * 2\n",
    "        # res是模拟的字符数组\n",
    "        res = [''] * newlen\n",
    "        i = newlen - 1\n",
    "        # 从右向左遍历\n",
    "        for c in S[:length][::-1]:\n",
    "            if c == ' ':\n",
    "                res[i] = '0'\n",
    "                res[i - 1] = '2'\n",
    "                res[i - 2] = '%'\n",
    "                i -= 3\n",
    "            else:\n",
    "                res[i] = c\n",
    "                i -= 1\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        ans = ''\n",
    "        k = length - len(''.join(S.split(' ')))\n",
    "        p = 0\n",
    "        while k:\n",
    "            if S[p] == ' ':\n",
    "                k -= 1\n",
    "                ans += '%20'\n",
    "                p += 1\n",
    "            else:\n",
    "                ans += S[p]\n",
    "                p += 1\n",
    "        while p < len(S) and S[p] != ' ':\n",
    "            ans += S[p]\n",
    "            p += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        a = []\n",
    "        for i in S:\n",
    "            if i == ' ':\n",
    "                a.append('%20')\n",
    "            else:\n",
    "                a.append(i)\n",
    "        # print(''.join(a))\n",
    "        return ''.join(a[:length])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        # 先统计一下空格的次数\n",
    "        count = 0\n",
    "        for i in range(length):\n",
    "            if S[i] == \" \":\n",
    "                count += 1\n",
    "        \n",
    "        new_res = [\"\" for i in range(length + 2 * count)]\n",
    "        j = length + 2 * count - 1\n",
    "        i = length - 1\n",
    "        while i >= 0 and j >= 0:\n",
    "            if S[i] != \" \":\n",
    "                new_res[j] = S[i]\n",
    "                i -= 1\n",
    "                j -= 1\n",
    "            else:\n",
    "                i -= 1\n",
    "                new_res[j] = \"0\"\n",
    "                new_res[j-1] = \"2\"\n",
    "                new_res[j-2] = \"%\"\n",
    "                j -= 3\n",
    "        return \"\".join(new_res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        S = list(S)\n",
    "        for i in range(len(S)):\n",
    "            if S[i]==\" \":\n",
    "                S[i] = \"%20\"\n",
    "        return \"\".join(S[0:length])\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        S = list(S)\n",
    "        for i in range(length):\n",
    "            if S[i]==' ':\n",
    "                S[i]='%20'\n",
    "        return ''.join(S[:length])\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        print(len(S))\n",
    "        tmp = []\n",
    "        for i in range(length):\n",
    "            if S[i] == ' ':\n",
    "                tmp.append(\"%20\")\n",
    "                continue\n",
    "            tmp.append(S[i])\n",
    "        print(tmp)\n",
    "        emp = ''\n",
    "        for i in tmp:\n",
    "            emp = emp+i\n",
    "        return(emp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S, length):\n",
    "        slist = list(S)\n",
    "        S_ = ''\n",
    "        for i in range(0, length):\n",
    "            if slist[i] == ' ':\n",
    "                S_ = S_ + '%20'\n",
    "            else:\n",
    "                S_= S_ + slist[i]\n",
    "        return S_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        S=list(S)\n",
    "        for i in range(length):\n",
    "            if S[i] == ' ':\n",
    "                S[i] = \"%20\"\n",
    "        S = S[:length]\n",
    "        return \"\".join(S)\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        words = S.split(\" \")\n",
    "        ret = \"\"\n",
    "        l = 0\n",
    "        for word in words:\n",
    "            l += len(word)\n",
    "            ret += word\n",
    "            if l < length:\n",
    "                ret += \"%20\"\n",
    "                l += 1\n",
    "        for i in range(length - l):\n",
    "            ret += \"%20\"\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        s_list = list(S)\n",
    "        res_list = []\n",
    "        for i in range(length):\n",
    "            if s_list[i] == ' ':\n",
    "                res_list.append('%20')\n",
    "            else:\n",
    "                res_list.append(s_list[i])\n",
    "        return ''.join(res_list)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        cnt = 0\n",
    "\n",
    "        arr =  S.split(' ')\n",
    "        res = ''\n",
    "\n",
    "        for i in range(len(arr)):\n",
    "            if cnt<length :\n",
    "                res+=arr[i]\n",
    "                cnt+=len(arr[i])      \n",
    "                if cnt< length:\n",
    "                    res+='%20'\n",
    "                    cnt+=1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        res = ['%20' if x == ' ' else x for x in S]\n",
    "        return ''.join(res[:length])\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        s = []\n",
    "        for i in range(length):\n",
    "            if S[i] == \" \":\n",
    "                s += \"%20\"\n",
    "            else:\n",
    "                s += S[i]\n",
    "        return \"\".join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        # offer消失法\n",
    "        # return S[:length].replace(' ', \"%20\") \n",
    "        res=[]\n",
    "        for i in range(length): \n",
    "            res += S[i] if S[i] != ' ' else \"%20\"\n",
    "        return ''.join(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        s = [i if i is not \" \" else \"%20\" for i in S]\n",
    "        return \"\".join(s[:length])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        # S=S.replace(' ',r'%20')\n",
    "        # return S[:min(length,len(S))]\n",
    "        res=[]\n",
    "        # le=-1\n",
    "        # ri=-1\n",
    "        for i,w in enumerate(S):\n",
    "            if w==' ':\n",
    "                res.append(r'%20')\n",
    "                # if ri==-1: ri=i\n",
    "            else:\n",
    "                res.append(w)\n",
    "                # if le==-1: le=i\n",
    "                # ri=-1\n",
    "        # print(res,le,ri,len(S))\n",
    "        return ''.join(res[:min(len(res),length)])\n",
    "        # if ri==0: return '%20'*min(len(S),length)\n",
    "        # L=0 if le==-1 else le\n",
    "        # R=len(res) if ri==-1 else ri\n",
    "        # return ''.join(res[L:R])\n",
    "        # S=S.strip()\n",
    "        # S=S.replace(' ',r'%20')\n",
    "        # return S"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        ls = S.split(' ')\n",
    "        # print(ls)\n",
    "        n=0\n",
    "        for i in range(length):\n",
    "            if S[i]==' ':\n",
    "                n+=1\n",
    "        res = '%20'.join(ls)\n",
    "        return res[:length+n*2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        # return S[:length].replace(' ', '%20')\n",
    "\n",
    "    \n",
    "        s = S[:length]\n",
    "        l = list(s.split(' '))\n",
    "        print(s.split(' '))\n",
    "        s = '%20'.join(l)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        list_S = list(S)\n",
    "        for i in range(length):\n",
    "            if list_S[i] == ' ':\n",
    "                list_S[i] = '%20'\n",
    "        ans = ''.join(list_S[:length])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        S = list(S)\n",
    "        for i in range(len(S)):\n",
    "            if length == 0:\n",
    "                S = S[:i]\n",
    "                break\n",
    "            if S[i] == ' ':\n",
    "                S[i] = '%20'\n",
    "            length -= 1\n",
    "        \n",
    "        S = ''.join(S)\n",
    "        return S"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        if len(S) == length:\n",
    "            return re.sub(r'\\s', '%20', S)\n",
    "        arr = [ch for ch in S]\n",
    "        while len(arr) != length:\n",
    "            arr.pop()\n",
    "\n",
    "        return re.sub(r'\\s', '%20', ''.join(arr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        ans=[]\n",
    "        for ch in S:\n",
    "            if ch==' ':\n",
    "                ans.append('%20')\n",
    "            else:\n",
    "                ans.append(ch)\n",
    "        return ''.join(ans[0:length])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# replace方法\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        S = S[:length].replace(' ', '%20')\n",
    "        return S\n",
    "\n",
    "# 列表+判断\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        s_list = list(S[:length])\n",
    "        ans = []\n",
    "        for i in range(len(s_list)):\n",
    "            if s_list[i] == ' ':\n",
    "                ans.append('%20')\n",
    "            else:\n",
    "                ans.append(s_list[i])\n",
    "        print(ans)\n",
    "        return (''.join(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        ls1 = []\n",
    "        for i in range(len(S)):\n",
    "            ls1.append(S[i])\n",
    "\n",
    "        if (length > len(ls1)):\n",
    "            ls1 += ['%20'] * (length-len(ls1))\n",
    "       \n",
    "        for i in range (len(ls1)):\n",
    "            if(ls1[i] == ' '):\n",
    "                ls1[i] = '%20'\n",
    "\n",
    "        s2 = ''\n",
    "        for j in range(length):\n",
    "            s2 += ls1[j]\n",
    "        return s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        # 倒序遍历\n",
    "        # S的长度只保证够用，而不是相等\n",
    "        S = [k for k in S]\n",
    "\n",
    "        j = len(S) - 1\n",
    "        i = length - 1\n",
    "\n",
    "        while(i >= 0):\n",
    "            if S[i] == \" \":\n",
    "                S[j] = \"0\"\n",
    "                S[j - 1] = \"2\"\n",
    "                S[j - 2] = \"%\"\n",
    "                j = j - 3\n",
    "            else:\n",
    "                S[j] = S[i]\n",
    "                j = j - 1\n",
    "            i -= 1\n",
    "\n",
    "        ans = \"\"\n",
    "        for i in S:\n",
    "            ans += i\n",
    "\n",
    "        return ans[j + 1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        S = ''.join(list(S)[:length])\n",
    "        return S.replace(' ', \"%20\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceSpaces(self, S: str, length: int) -> str:\n",
    "        str_list = []\n",
    "        for i, n in enumerate(list(S)):\n",
    "            if i < length:\n",
    "                str_list.append(n)\n",
    "        tmp = []\n",
    "        for i in str_list:\n",
    "            if i == \" \":\n",
    "                tmp.append(\"%20\" )\n",
    "            else:\n",
    "                tmp.append(i)\n",
    "        return \"\".join(tmp)\n",
    "\n",
    "\n",
    "            "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
